from neo4j import GraphDatabase

# -----------------------------
# 配置 Neo4j 连接
# -----------------------------
NEO4J_URI = "bolt://localhost:7687"  # Bolt 协议端口
NEO4J_USER = "neo4j"
NEO4J_PASSWORD = "Fnst@123"           # 你在 Docker 设置的密码

# 创建驱动
driver = GraphDatabase.driver(NEO4J_URI, auth=(NEO4J_USER, NEO4J_PASSWORD))

# -----------------------------
# 查询 Alice 的朋友喜欢的电影
# -----------------------------
def recommend_movies_for_alice(tx):
    query = """
    MATCH (alice:Person {name:'Alice'})-[:FRIEND]->(friend)-[:LIKES]->(movie)
    WHERE NOT (alice)-[:LIKES]->(movie)
    RETURN friend.name AS Friend, movie.title AS Movie
    """
    result = tx.run(query)
    return [(record["Friend"], record["Movie"]) for record in result]

# -----------------------------
# 主程序
# -----------------------------
if __name__ == "__main__":
    with driver.session() as session:
        recommendations = session.read_transaction(recommend_movies_for_alice)
        print("Alice 的朋友推荐电影：")
        for friend, movie in recommendations:
            print(f"- {movie} (被 {friend} 喜欢)")

    driver.close()
